package com.cosmoplat.xikai.business.controller.mes;

import com.alibaba.fastjson2.JSONObject;
import com.cosmoplat.xikai.business.service.BaseDeliveryInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName MesXikaiTestController
 * @Description mes测试接口
 * @Author wdf
 * @Date 2025/1/17
 */
@RestController
@Slf4j
@RequestMapping("xikaiMes")
public class MesXikaiTestController {

    @Autowired
    private BaseDeliveryInfoService baseDeliveryInfoService;

    @RequestMapping("/test")
    public String test(){
        log.info("xikaiMes test");
        return "xikaiMes test";
    }

    @PostMapping("/materialInit")
    public JSONObject materialInit(@RequestBody JSONObject jsonObject){
        log.info("xikaiMes materialInit");
        return JSONObject.parseObject("");
    }



    /** 2.15.物料配送接口 */
    private String materialDistribution;


    /** 2.13.产线报警接口 */
    private String productLineAlarm;

    /** 2.14.报警关闭接口 */
    private String closeAlarm;

    /** 2.24.器具物料信息查询接口 */
    private String scanCodeToMaterialQty;

    /** 2.17.任务完工与工时汇报接口 */
    private String finishTask;

    int i = 1;

    private String ok = "{\n" +
            "    \"code\": 200,\n" +
            "    \"message\": \"\",\n" +
            "    \"data\": null\n" +
            "}";
    /**
     *  生产开工接口
     * @date 10:40 2025/1/17
     * @param jsonObject
     * @return com.alibaba.fastjson2.JSONObject
     **/
    @PostMapping("/startWork")
    public JSONObject startWork(@RequestBody JSONObject jsonObject){
        log.info("{}", jsonObject);
        log.info("xikaiMes startWork");
        return JSONObject.parseObject(ok);
    }

    /** 2.15.物料配送接口 */
    @PostMapping("/materialDistribution")
    public JSONObject materialDistribution(@RequestBody JSONObject jsonObject){
        log.info("{}", jsonObject);
        log.info("xikaiMes materialDistribution");
        return JSONObject.parseObject(ok);
    }

    /** 2.13.产线报警接口 */
    @PostMapping("/productLineAlarm")
    public JSONObject productLineAlarm(@RequestBody JSONObject jsonObject){
        log.info("{}", jsonObject);
        log.info("xikaiMes productLineAlarm");
        return JSONObject.parseObject(ok);
    }

    /** 2.14.报警关闭接口 */
    @PostMapping("/closeAlarm")
    public JSONObject closeAlarm(@RequestBody JSONObject jsonObject){
        log.info("{}", jsonObject);
        log.info("xikaiMes closeAlarm");
        return JSONObject.parseObject(ok);
    }

    /** 2.17.任务完工与工时汇报接口*/
    @PostMapping("/finishTask")
    public JSONObject finishTask(@RequestBody JSONObject jsonObject){
        log.info("{}", jsonObject);
        log.info("xikaiMes finishTask");
        return JSONObject.parseObject(ok);
    }

    /** 2.24.器具物料信息查询接口 */
    @PostMapping("/scanCodeToMaterialQty")
    public JSONObject scanCodeToMaterialQty(@RequestBody JSONObject jsonObject){
        log.info("{}", jsonObject);
        String sku = "BG893.1P-47";
        if (i++ % 2 == 0) {
            sku = "8KA.370.734";
        }
        sku = "错误物料测试";
        log.info("xikaiMes scanCodeToMaterialQty");
        String res = "{\n" +
                "  \"code\": 200,\n" +
                "  \"message\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"businessdata\": [\n" +
                "      {\n" +
                "        \"sku\":\"" + sku + " \",\n" +
                "        \"qty\": 10,\n" +
                "        \"batchno\": \"批次号\"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
        return JSONObject.parseObject(res);
    }

    /**
     * 测试扫码 - 模拟扫码的过程
     */
    @PostMapping("/scanCode")
    public JSONObject scanCode(@RequestBody JSONObject jsonObject){
        try {
            log.info("{}", jsonObject);
            log.info("xikaiMes scanCode");
            baseDeliveryInfoService.updateStockQtyForScanCode(jsonObject.getString("qrCode"));
        } catch (Exception e) {
            log.error("扫码异常", e);
        }
        return JSONObject.parseObject(ok);
    }

}
